/** UserModel */
package server.service.user;

import java.util.List;

import server.service.patient.PatientModel;
import domain.exception.ValidationException;
import domain.patient.Patient;
import domain.user.SystemUser;
import domain.user.User;

/**
Interacts with UserDao and panels
 * @author jessienewman
 * @version $Revision: 1.0 $
 */
public class UserModel {
	/**
	 * Field dao.
	 */
	private UserDao dao;

	/**
	 * Constructor for UserModel.
	 */
	public UserModel() {
		dao = new UserDao();
	}

	/**
	 * Method isValidUsername.
	 * @param username String
	
	 * @return Boolean */
	public Boolean thisIsValidUsername(String username) {
		return dao.thisIsValidUsername(username);
	}

	/**
	 * Method loginValid.
	 * @param username String
	 * @param password String
	
	 * @return Boolean */
	public Boolean loginValid(String username, String password) {
		return dao.loginValid(username, password);
	}

	/**
	 * Method getUserId.
	 * @param username String
	
	 * @return Integer */
	public Integer getUserId(String username) {
		return dao.getUserId(username);
	}

	/**
	 * Method getUser.
	 * @param userId Integer
	
	 * @return User */
	public User getUser(Integer userId) {
		return dao.getUser(userId);
	}

	/**
	 * Method modifyUsername.
	 * @param userId Integer
	 * @param username String
	 */
	public void modifyUsername(Integer userId, String username) {
		dao.modifyUsername(userId, username);
	}

	/**
	 * Method assertMatchingPasswords.
	 * @param password String
	 * @param password2 String
	
	 * @throws ValidationException */
	public void assertMatchingPasswords(String password, 
			String password2) throws ValidationException {
		if (!password.equals(password2)) {
			throw new ValidationException("Passwords do not match."); // $codepro.audit.disable nullPointerDereference
		}
	}

	/**
	 * Method modifyPassword.
	 * @param userId Integer
	 * @param password String
	 * @param password2 String
	
	 * @throws ValidationException */
	public void modifyPassword(Integer userId, String password, 
			String password2) throws ValidationException{
			assertMatchingPasswords(password, password2);
			dao.modifyPassword(userId, password);
	}
	
	/**
	 * Method validateUser.
	 * @param u SystemUser
	
	 * @throws ValidationException */
	public void validateUser(final SystemUser u) throws ValidationException {
		String msg = "";
		
		if (u.getNameFirst().equals("")) {
			msg += "First name is empty. \n";
		}
		
		if (u.getNameLast().equals("")) {
			msg += "Last name is empty. \n";
		}

		if (!msg.isEmpty()) {
			throw new ValidationException(msg); // $codepro.audit.disable nullPointerDereference
		}
		
	}

	/**
	 * Method addUser.
	 * @param u User
	 * @param username String
	 * @param p1 String
	 * @param p2 String
	 * @return Integer  * 
	 * @throws ValidationException 
	 */
	public Integer addUser(final User u, String username, 
			String p1, String p2) throws ValidationException {
		UserModel um = new UserModel();
			um.assertMatchingPasswords(p1, p1);
			if (u instanceof Patient) {
				PatientModel pm = new PatientModel();
				pm.validatePatient((Patient) u);
			} else {
				validateUser((SystemUser) u);
			}
			return dao.addUser(u);
	}
	
	/**
	 * Method findUsers.
	 * @param s String
	
	 * @return List */
	public List<User> findUsers(String s) {
		return dao.findUsers(s);
	}

	/**
	 * Method modifyName.
	 * @param id Integer
	 * @param nameFirst String
	 * @param nameMiddle String
	 * @param nameLast String
	
	 * @throws ValidationException */
	public void modifyName(Integer id, String nameFirst, 
			String nameMiddle, String nameLast) 
	throws ValidationException{
		if (nameFirst.equals("")) {
			throw new ValidationException("Please include a First Name"); // $codepro.audit.disable nullPointerDereference
		}
		if (nameLast.equals("")) {
			throw new ValidationException("Please include a Last Name"); // $codepro.audit.disable nullPointerDereference
		}
		dao.modifyNameFirst(id, nameFirst);
		dao.modifyNameMiddle(id, nameMiddle);
		dao.modifyNameLast(id, nameLast);
	}
	
	/**
	 * Method setActive.
	 * @param id Integer
	 * @param active Boolean
	 */
	public void setActive(Integer id, Boolean active) {
		dao.setActive(id, active);
	}
	
	/**
	 * super's toString
	 * @return super's toString */
	public String toString() {
		return super.toString();
	}
}


